package vn.nt.healthcare.server.ws.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import vn.nt.healthcare.server.ws.model.Schedule;
import vn.nt.healthcare.server.ws.service.ScheduleService;

@Controller
public class ScheduleController {

	@Autowired
	private ScheduleService scheduleService;

	@RequestMapping(value = "/listOrdersInDay", method = RequestMethod.GET)
	@ResponseBody
	public List<Schedule> findAllOrdersInDayOfDoctor(
			HttpServletRequest request,
			@RequestParam(value = "companyId", required = true) String companyId,
			@RequestParam(value = "status") String status,
			@RequestParam(value = "staffId") int staffId) {
		return scheduleService.findSchedulesByStaffInDate(companyId, status,
				staffId);
	}

	@RequestMapping(value = "/schedule/listInTime", method = RequestMethod.GET)
	@ResponseBody
	public List<Schedule> findAllOrdersInTimeOfDoctor(
			HttpServletRequest request,
			@RequestParam(value = "companyId", required = true) String companyId,
			@RequestParam(value = "status") String status,
			@RequestParam(value = "staffId") int staffId,
			@RequestParam(value = "morning") boolean isMorning) {
		return scheduleService.findSchedulesInTime(companyId, status, staffId,
				isMorning);
	}

	@RequestMapping(value = "/schedule/listByCustomer", method = RequestMethod.GET)
	@ResponseBody
	public List<Schedule> findAllOrdersByCustomer(
			HttpServletRequest request,
			@RequestParam(value = "companyId", required = true) String companyId,
			@RequestParam(value = "status") String status,
			@RequestParam(value = "customerId") int customerId,
			@RequestParam(value = "staffId") int staffId) {
		return scheduleService.findSchedulesByCustomer(companyId, status,
				customerId, staffId);
	}

	@RequestMapping(value = "/schedule/order", method = RequestMethod.POST)
	@ResponseBody
	public Schedule order(
			HttpServletRequest request,
			@RequestParam(value = "companyId", required = true) String companyId,
			@RequestParam(value = "orderTime", required = true) Date orderTime,
			@RequestParam(value = "content") String content,
			@RequestParam(value = "customerId") int customerId,
			@RequestParam(value = "staffId") int staffId,
			@RequestParam(value = "subjectId") int subjectId) {
		return scheduleService.order(companyId, staffId, orderTime, content,
				customerId, subjectId);
	}

	@RequestMapping(value = "/schedule/update", method = RequestMethod.PUT)
	@ResponseBody
	public Schedule update(
			HttpServletRequest request,
			@RequestParam(value = "companyId", required = true) String companyId,
			@RequestParam(value = "orderTime", required = true) Date orderTime,
			@RequestParam(value = "id") int id,
			@RequestParam(value = "status") String status) {
		return scheduleService.updateSchedule(companyId, id, status, orderTime);
	}

	@RequestMapping(value = "/schedule/destroy", method = RequestMethod.DELETE)
	@ResponseBody
	public String destroy(
			HttpServletRequest request,
			@RequestParam(value = "companyId", required = true) String companyId,
			@RequestParam(value = "id") int id) {
		boolean result = scheduleService.destroySchedule(companyId, id);
		if (result) {
			return "Destroy successfully";
		} else {
			return "Destroy order failed";
		}
	}
	
	@RequestMapping(value = "/schedule/detail", method = RequestMethod.GET)
	@ResponseBody
	public Schedule findScheduleById(
			HttpServletRequest request,
			@RequestParam(value = "companyId", required = true) String companyId,
			@RequestParam(value = "id") int id) {
		return scheduleService.findScheduleById(companyId, id);
	}
}
